﻿using AdvancedCollections;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AdvancedCollectionsPerformanceTests
{
    public class TreePerformanceTest : IPerformanceTest
    {
        private const int DataCount = 30000;
        private const int FillRandomSeed = 42;
        private const int RandomSeed = 7;
        private const int SingleItemChecks = 10000;

        private TreeBasePerformanceTest[] tests = {
                                              /* new ListTreePerformanceTest("List"),
                                              new SortedSetPerformanceTest("SortedSet"), */
                                              new BinaryTreePerformanceTest("BinaryTree"),
                                              new AVLTreePerformanceTest("AVLTree")
                                            };

        public void RunTest()
        {
            RunOrderedTest();
            RunRandomizedTest();
        }

        private void CheckRandomContains(int randomSeed, int singleItemChecks)
        {
            Console.WriteLine("Checking if {0} random values exist in a collection:", singleItemChecks);

            foreach (var item in tests)
            {
                item.CheckRandomContains(randomSeed, singleItemChecks);
            }
        }

        private void FillDataSources(IEnumerable<int> dataSet)
        {
            Console.WriteLine("Initialization of {0} data:", DataCount);

            foreach (var item in tests)
            {
                item.FillDataSources(dataSet);
            }
        }

        private void FillDataSources(int fillRandomSeed, int dataCount)
        {
            Console.WriteLine("Initialization of {0} data:", dataCount);

            foreach (var item in tests)
            {
                item.FillDataSources(fillRandomSeed, dataCount);
            }
        }

        private void RemoveRandomValues(int randomSeed, int singleItemChecks)
        {
            Console.WriteLine("Removing {0} random values from the collections:", singleItemChecks);

            foreach (var item in tests)
            {
                item.RemoveRandomValues(randomSeed, singleItemChecks);
            }
        }

        private void RunOrderedTest()
        {
            Console.WriteLine("##### BinaryTreePerformanceTest with ordered Data");
            FillDataSources(Enumerable.Range(1, DataCount));
            CheckRandomContains(RandomSeed, SingleItemChecks);
            RemoveRandomValues(RandomSeed, SingleItemChecks);
        }

        private void RunRandomizedTest()
        {
            Console.WriteLine("##### BinaryTreePerformanceTest with random Data");
            FillDataSources(FillRandomSeed, DataCount * 100);
            CheckRandomContains(RandomSeed, SingleItemChecks);
            RemoveRandomValues(RandomSeed, SingleItemChecks);
        }
    }
}